package com.example.qu_tao.View;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.qu_tao.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XiaoxiFragment extends Fragment {

    private List<RelativeLayout> messageList;
    private Map<RelativeLayout, String> messageNameMap;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_xiaoxi, container, false);

        // 初始化消息列表和名称映射
        messageList = new ArrayList<>();
        messageNameMap = new HashMap<>();

        // 添加消息项并设置tag
        addMessageItem(view, R.id.message1, "海绵宝宝");
        addMessageItem(view, R.id.message2, "派大星");
        addMessageItem(view, R.id.message3, "章鱼哥");
        addMessageItem(view, R.id.message4, "珊迪");
        addMessageItem(view, R.id.message5, "蟹老板");

        // 监听搜索框文本变化
        EditText searchEditText = view.findViewById(R.id.searchEditText);
        searchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                filterMessages(s.toString().trim());
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });

        // 搜索按钮点击事件
        ImageButton searchButton = view.findViewById(R.id.searchButton);
        searchButton.setOnClickListener(v -> filterMessages(searchEditText.getText().toString().trim()));

        return view;
    }

    private void addMessageItem(View view, int id, String name) {
        RelativeLayout message = view.findViewById(id);
        message.setTag(name);
        messageList.add(message);
        messageNameMap.put(message, name);

        // 设置点击事件
        message.setOnClickListener(v -> openMessageActivity(name));
    }

    private void filterMessages(String searchText) {
        if (searchText.isEmpty()) {
            // 搜索框为空时显示所有消息
            for (RelativeLayout message : messageList) {
                message.setVisibility(View.VISIBLE);
            }
        } else {
            // 根据搜索内容过滤显示
            for (RelativeLayout message : messageList) {
                String name = messageNameMap.get(message);
                if (name != null && name.contains(searchText)) {
                    message.setVisibility(View.VISIBLE);
                } else {
                    message.setVisibility(View.GONE);
                }
            }
        }
    }

    private void openMessageActivity(String receiverName) {
        Intent intent = new Intent(getContext(), MessageActivity.class);
        intent.putExtra("name", receiverName);
        startActivity(intent);
    }
}